package com.fqh.controller;

import com.fqh.entity.Foodclass;
import com.fqh.entity.JsonResult;
import com.fqh.service.FoodClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class FoodClassController {

    @Autowired
    private FoodClassService foodClassService;

    @RequestMapping("findAll.do")
    public Map findAll(int page, int limit) {
        List<Foodclass> list = foodClassService.findByPage(page, limit);
        Integer count = foodClassService.findCount();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list);
        map.put("count", count);
        return map;
    }

    @PostMapping("findFoodClassAndFoodInfo.do")
    public Map findFoodClassAndFoodInfo() {
        Map<String, Integer> map = new HashMap<String, Integer>();
        List<Integer> listCount = new ArrayList<>();
        List<Foodclass> list = foodClassService.findFoodClass();
        for (int i = 0; i < list.size(); i++) {
            Integer info = foodClassService.findFoodInfo(list.get(i).getClassId());
            listCount.add(info);
        }
        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i).getClassName(), listCount.get(i));
        }
        return map;
    }

    @RequestMapping("findFoodClassById.do")
    public Foodclass findFoodClassById(Integer classId) {
        Foodclass foodClass = foodClassService.findFoodClassById(classId);
        return foodClass;
    }

    @RequestMapping("updateFoodClass.do")
    public JsonResult updateFoodClass(String foodId, String className) {
        Foodclass foodclass = new Foodclass();
        foodclass.setClassId(Integer.parseInt(foodId));
        foodclass.setClassName(className);
        JsonResult result = null;
        try {
            Integer integer = foodClassService.updateById(foodclass);
            result = new JsonResult("修改成功", true);
        } catch (Exception e) {
            result = new JsonResult("修改失败", false);
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("addFoodClass.do")
    public JsonResult addFoodClass(String className) {
        JsonResult result = null;
        try {
            Integer integer = foodClassService.insert(className);
            result = new JsonResult("添加成功", true);
        } catch (Exception e) {
            result = new JsonResult("添加失败", false);
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/deleteById.do")
    public JsonResult deleteById(String id) {
        JsonResult result = null;
        try {
            Integer integer = foodClassService.deleteById(Integer.parseInt(id));
            result = new JsonResult("删除成功", true);
        } catch (Exception e) {
            result = new JsonResult("删除失败,该菜系下面有菜品,请先删除菜品", false);
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/findAllFoodClassName.do")
    public List<Foodclass> findAllFoodClassName() {
        List list1 = new ArrayList();
        List<Foodclass> list = foodClassService.findFoodClass();
        for (Foodclass foodclass : list) {
            String className = foodclass.getClassName();
            list1.add(className);
        }
        return list1;
    }

}
